package student.aistar.day11.homework;

import java.util.*;

public class CountStrDemo {
    public static void main(String[] args) {
        String str = "sadnjsadnsad";
        count(str);
    }

    private static void count(String str) {
        Map<Character, Integer> characterIntegerMap = new HashMap<>();

        for(int i = 0; i < str.length(); i++){
            if(characterIntegerMap.containsKey(str.charAt(i))){
                int count = characterIntegerMap.get(str.charAt(i));
                characterIntegerMap.put(str.charAt(i), count + 1);
            }else
                characterIntegerMap.put(str.charAt(i), 1);
        }

        List<Map.Entry<Character, Integer>> sortList = new ArrayList<>();
        Set<Map.Entry<Character, Integer>> entries = characterIntegerMap.entrySet();
        Iterator<Map.Entry<Character, Integer>> iterator = entries.iterator();
        while (iterator.hasNext()){
            Map.Entry<Character, Integer> next = iterator.next();
            sortList.add(next);
        }

        sortList.sort((m1, m2) -> m2.getValue() - m1.getValue());

        System.out.println(sortList);
    }
}
